const app = getApp()

Page({
    data: {
        xPageReady : true ,
        xRegister : false ,
        xBuyState : false ,
        xNeedRead : true ,
        xUserInfo : '' ,
        xProduct : '' ,
        xUnKnow : '' ,
        xDoctor : '' ,
        xRead: '' ,
        xPid: '' ,
        xTes: false ,
        week:'',
        day:''

    },
  
    onLoad: function (options) {
        let that = this
        that.setData({
          week:Number(options.week),
          day:Number(options.day)
        })
    },

    onShow: function () {
        this.xInit();
        this.xInfo()
    },
    xToPlay: function (e) {
      let params = e.currentTarget.dataset
      console.log(params)
      wx.navigateTo({
          url: '/subtool/pages/rudio/detail/index',
          success: function(res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('xToAudioDetail', params)
          }
      })
    },
    todayremb: function(e){
      let name =e.currentTarget.dataset.name;
      let id =e.currentTarget.dataset.id;
      let num =e.currentTarget.dataset.num;
      let img_url="";
      let color="";
      switch(name) {
        case "情绪日记":
            img_url='https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/qxrj.png';
            color='background-color:#E6F2FF'
        break;
        case "焦虑情绪日记":
            img_url='https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/jlqxrj.png';
            color='background-color:#FAD9D9'
        break;
        case "思想日记":
            img_url='https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/sxrj.png';
            color='background-color:#D5EBE9'
        break;
        case "管理反刍式思维":
            img_url='https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/glrj.png';
            color='background-color:#FFEDD5'
        break;
        case "认知日记":
            img_url='https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/rzrj.png';
            color='background-color:#E6E6FF'
        break;
        case "行为日记":
            img_url='https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/xwrj.png';
            color='background-color:#EEE7F4'
        break;
        default:
            img_url='https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/khrj.png';
            color='background-color:#FAD9D9'
      } 
      wx.navigateTo({
        url: '/subtool/pages/dayremb/detdayremb/index?id=' + id + '&num=' + num + '&week='+this.data.week +'&title=' + name+'&color='+color+'&icon='+img_url+'&day='+this.data.day+'&treatment_item_id='+id,
      })
    },
    // torudio:function(){
    //     wx.navigateTo({
    //       url: '/subtool/pages/rudio/index/index',
    //     })
    // },
    xtoProduct :function(){
        wx.navigateTo({
          url: '/subsale/pages/product/index/index?id='+this.data.xProductnew.additional.product_id
        })
    },
    // 初始化
    xInit: async function () {
        let that = this
        
        await wx.cloud.callFunction({
          name: 'digital_therapy',
          data: {
            action: "get_going_project"
          }
        }).then(res => {
          that.setData({
            xProductnew:res.result
          })
        })

        wx.cloud.callFunction({
            name: 'digital_therapy',
            data: {
                action: "get_treatment_item_by_day",
                dataObj:{
                  week:that.data.week,
                  day:that.data.day
                }
            }
        }).then(res => {
          that.setData({
            xProductnew:res.result
          })
          that.setData({
            ['xProductnew.treatment_item_content']:res.result
          })
        })
        
        that.setData({
          xPageReady : false
        })
        
    },

    // 用户数据检查
    xInfo: function () {
        let that = this
        let key = app.globalData.xRegister
        let state = false
        wx.cloud.callFunction({
            name : 'user' ,
            data : {
                action : 'detail'
            }
        }).then( res => {
            // console.log('用户' , res.result)
            let user = res.result
            for(var i = 0 ; i < key.length ; i++){
                if(user[key[i]] == '' || user[key[i]] == undefined){
                    state = true
                }else{
                    state = false
                }
            }
            that.setData({
                xRegister : state ,
                xUserInfo : user ,
                xDoctor : user.consultation_teacher_info[0]
            })
        })
    },


    // 前往全部阶段
    xToProce: function () {
        let that = this
        let id = that.data.xPid
        wx.navigateTo({
            url: '/subsale/pages/product/proce/index?id=' + id,
        })
    },

    // 前往聊天 - 会话 - 交流
    xToMessage: function (e) {
        let params = e.currentTarget.dataset
        let id = params.id
        let ava = params.ava
        let name = params.name
        wx.navigateTo({
            url: '/subsale/pages/product/chat/index?id=' + id + '&name=' + name + '&ava=' + ava,
        })
    },

    xToTech: function (e) {
        let data = e.currentTarget.dataset
        let params = {
            index: data.index ,
            content: data.content ,
            week: data.week ,
            ava : data.ava ,
            cid : data.cid ,
            day: data.day,
            education_content_id: data.education_content_id
        }
        wx.navigateTo({
            url: '/subsale/pages/product/tech/index',
            success: function (res) {
                res.eventChannel.emit('xToTecDetail', params)
            }
        })
    },

    // 作业 => 日记
    xToDayremb: function (e) {
        let that = this
        let week = that.data.week
        let num = e.currentTarget.dataset.id
        let title = e.currentTarget.dataset.title
        wx.navigateTo({
            url: '/subtool/pages/dayremb/detail/index?num=' + num + '&week=' + week + '&title=' + title,
        })
    },

    // 作业 => 音频
    xToRudio: function (e) {
        let that = this
        let id = e.currentTarget.dataset.id
        wx.cloud.callFunction({
            name: 'order_task_func',
            data: {
                action: 'get_muses'
            }
        }).then(res => {
            let data = res.result
            for (var i in data) {
                if (data[i]._id === id) {
                    let params = data[i]
                    params.week = that.data.xProduct.additional.treatment_stage
                    wx.navigateTo({
                        url: '/subtool/pages/rudio/detail/index',
                        success: function (res) {
                            // 通过eventChannel向被打开页面传送数据
                            res.eventChannel.emit('xToAudioDetail', params)
                        }
                    })
                }
            }
        })
    },

    // 作业 => 测评
    xToEva: function () {
        let that = this
        let id = 'all'
        let week = that.data.xProduct.additional.treatment_stage
        wx.navigateTo({
            url: '/subsale/pages/evl/detail/index?id=' + id + '&week=' + week + '&okey=other'+'&day='+that.data.xProductnew.additional.day,
        })
    },

    // 前往用户页面
    xToUserDetail: function () {
        wx.navigateTo({
            url: '/subuser/pages/information/index',
        })
    },

    // 是否完成阅读
    xWarning: function () {
        wx.showToast({
            title: '请先完成课件阅读',
            icon: 'none'
        })
    },

    // 前往咨询师
    xToMyDoctor: function (e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/subdter/pages/doctor/index/index?id=' + id,
        })
    },

    // 前往随堂测试
    xToTechEvl: function (e) {
        let that = this
        let params = e.currentTarget.dataset
        let product = that.data.xProduct
        if(that.data.xTes==true){
          wx.navigateTo({
            url: '/subsale/pages/techevl/index/index?id=' + params.id + 
            '&week=' + params.week +'&product_id='+ params.product_id 
          })
        }else{
          wx.navigateTo({
            url: '/subsale/pages/techevlR/index/index?id=' +  params.product_id + 
            '&week=' + params.week 
          })
        }
        // let endState = params.week === product.product_item_details.length
        
    },

    // 前往服务页
    xToIndex: function () {
      wx.navigateTo({
        url: '/pages/navigation/service/index',
      })
    }
})